/*
	Test program query functions.
*/

#include "Tests.h"

#ifdef _NOISE_UNIT_TEST

void TestProgram (Program prog, EProgram type)
{
	struct ProgQuery {
		EProgramQuery	query;
		uint			type;
	};

	const uint ALL_PROGRAMS = -1;

	const ProgQuery iqueries[] = {
		{ PROGRAM_BUILD_STATE,			ALL_PROGRAMS },
		{ PROGRAM_TYPE,					ALL_PROGRAMS },
		{ PROGRAM_OUT_FORMAT,			ALL_PROGRAMS },
		{ PROGRAM_BUILD_LOG_LENGTH,		1 << OPENCL_PROGRAM },
		{ PROGRAM_COMPILE_LOG_LENGTH,	1 << OPENCL_PROGRAM },
		{ PROGRAM_SOURCE_LENGTH,		1 << OPENCL_PROGRAM },
		{ PROGRAM_BINARY_LENGTH,		0 },
		{ PROGRAM_CL_POINTER,			1 << OPENCL_PROGRAM },
		{ PROGRAM_BUILD_OPTIONS,		ALL_PROGRAMS },
		{ PROGRAM_MAX_IMAGE_SIZE,		ALL_PROGRAMS },
		{ PROGRAM_NUM_IMAGE_FORMATS,	ALL_PROGRAMS },
		{ PROGRAM_IMAGE_FORMATS,		ALL_PROGRAMS }
	};

	const ProgQuery squeries[] = {
		{ PROGRAM_BUILD_LOG,			1 << OPENCL_PROGRAM },
		{ PROGRAM_COMPILE_LOG,			1 << OPENCL_PROGRAM },
		{ PROGRAM_SOURCE,				1 << OPENCL_PROGRAM },
		{ PROGRAM_BINARY,				1 << OPENCL_PROGRAM }
	};

	int		ivalues[256] = {0};
	char	cvalues[1<<14] = {0};

	Source	src = nCreateSource( CONSTANT );
	CHECK_N( nBuildProgram( prog, src, DEFAULT_OPTIONS ) );

	for (uint i = 0; i < count_of(iqueries); ++i)
	{
		if ( EnumNoCmp( iqueries[i].type, 1 << type ) )
			continue;

		CHECK_N( nGetProgramInteger( prog, iqueries[i].query, ivalues, count_of(ivalues) ) );
	}

	for (uint i = 0; i < count_of(squeries); ++i)
	{
		if ( EnumNoCmp( squeries[i].type, 1 << type ) )
			continue;

		CHECK_N( nGetProgramString( prog, squeries[i].query, cvalues, count_of(cvalues) ) );
	}

	nDeleteSource( src );
}


void ProgramTest()
{
	Program		cl_prog		= nCreateProgram( OPENCL_PROGRAM );
	Program		native_prog	= nCreateProgram( NATIVE_PROGRAM );

	TestProgram( cl_prog, OPENCL_PROGRAM );
	TestProgram( native_prog, NATIVE_PROGRAM );

	nDeleteProgram( cl_prog );
	nDeleteProgram( native_prog );
}

#endif	// _NOISE_UNIT_TEST